export const uploadImageUrL = "/media/file/uploadImage";
import { useMemberStore } from "@/stores";
export const uploadFileUrL = "/media/file/uploadFile";

/**
 * 选择文件
 * @param type 上传的数据类型
 * @param allowFileList
 * @returns
 */
export async function selectFileGUrl(type: any, allowFileList: any) {
  return new Promise((resolve, reject) => {
    uni.chooseMessageFile({
      count: 1,
      type: type, // "file",
      extension: allowFileList, //  ["pdf"],
      success: (res) => {
        console.log("res-uploadfile:", res);
        // 获取上传文件名称
        const currFileName = res.tempFiles[0].name;
        // 获取回显文件地址
        const contentUrl = res.tempFiles[0].path;
        var result = {
          contentUrl: contentUrl,
          currFileName: currFileName,
        };
        resolve(result);
      },
      fail: (err) => {
        console.error("选择文件失败:", err);
        reject(err);
      },
    });
  });
}
/**
 *
 * @param contentUrl contentUrl临时地址
 * @param name  "上传的content的key名称"
 * @param fileType ,image为图像，pdf为资源
 * @returns
 */
export async function uploadFileToServer(
  contentUrl: any,
  name: any,
  fileType: any
) {
  const memberStore = useMemberStore();
  var selectedServerUrl = "";
  if (fileType === "image") {
    selectedServerUrl = uploadImageUrL;
  } else if (fileType === "pdf") {
    selectedServerUrl = uploadFileUrL;
  } else {
    console.log("未进行指定上传的是什么文件类型");
    return;
  }
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: selectedServerUrl,
      filePath: contentUrl,
      name: name,
      header: {
        "content-type": "multipart/form-data",
        authorization: memberStore.profile.authorization,
      },
      success: function (res) {
        const resData = JSON.parse(res.data);
        console.log("resData", resData, "withoutParse:resData", res.data);
        // 上传后的地址
        const resultUrl = resData.data;
        var result = Object.assign(res);
        // 包装器
        var wrapper: any = {};
        wrapper.url = resultUrl;
        result.wrapper = wrapper;
        resolve(result);
      },
      fail: function (err) {
        reject(err);
      },
      complete: function (result) {
        reject(result);
        console.log("errMsg", result.errMsg);
      },
    });
  });
}

/**
 *
 * @param type 上传的数据类型，如果为文件，则填写file，（图片填写image
 * @param name 上传过程中，文件数据对应的key名称，一般为file或者image
 * @param allowFileList 允许上传的filter
 * @param fileType image为图像，pdf为资源，根据此选择上传的服务器
 * @returns
 */
export async function uploadFile(
  type: any,
  name: any,
  allowFileList: any,
  fileType: any
) {
  var res: any = await selectFileGUrl(type, allowFileList);
  // 拿到临时上传的值
  const contentUrl = res.contentUrl;
  const currFileName = res.currFileName;

  if (contentUrl !== null) {
    // 调用上传临时文件到服务器的业务函数
    var uploadRes: any = await uploadFileToServer(contentUrl, name, fileType);
    console.log("uploadRes", uploadRes);
    if (uploadRes.statusCode == 200) {
      uni.showToast({
        title: "上传成功",
        icon: "none",
        duration: 1500,
      });
      uploadRes.wrapper.currFileName = currFileName;
      return uploadRes;
    } else {
      uni.showToast({
        title: "上传失败",
        icon: "none",
        duration: 2000,
      });
    }
  } else {
    // 展示失败
    uni.showToast({
      title: "上传失败",
      icon: "none",
      duration: 2000,
    });
  }
}
